"""
Wrap of Phonon's media object methods

/file pkPlayback.py
/authors pykodio team
/date 22-10-2013
"""

from PyQt4.phonon import Phonon


class pkPlayback():
	"""
	General playback utility for pykodio.
	Uses phonon for playback.
	"""

	def __init__(self, parent=None):
		self._SEEKING_SPEED = 6000
		self._media_object = Phonon.MediaObject()
		self._audio_output = Phonon.AudioOutput(Phonon.MusicCategory, parent)
		Phonon.createPath(self._media_object, self._audio_output)


	def play(self):
		""" Start the playback """
		self._media_object.play()


	def pause(self):
		""" Pause the playback """
		self._media_object.pause()


	def stop(self):
		""" Stop the playback, clear the playlist, clear current source """
		self._media_object.stop()


	def rewind(self):
		"""
		Seek gradually the playing song backward
		/note This method should be place in a loop
		"""
		self._media_object.seek(-self._SEEKING_SPEED)


	def forward(self):
		"""
		Seek gradually the playing song forward
		/note This method should be placed in a loop
		"""
		self._media_object.seek(self._SEEKING_SPEED)


	def seek(self, time):
		"""
		Seek the playing song at a specified time
		/note Better use SeekSlider class
		"""
		self._media_object.seek(time)



	def clear(self):
		""" Empty Phonon's MediaObject queue and playing song"""
		self._media_object.clear()

	def get_elapsed_time(self):
		""" Return the elapsed time since the beginning of the playing song """
		return self._media_object.currentTime()


	def set_queue(self, paths_of_songs_to_enqueue=[]):
		"""
		Set Phonon's MediaObject queue to paths_of_songs_to_enqueue list
		/note The current Playing song won't be modified
		"""
		self._media_object.clearQueue()
		for path in paths_of_songs_to_enqueue:
			media_source = Phonon.MediaSource(path)
			self._media_object.enqueue(media_source)


	def get_queue(self):
		"""
		Return the current queue for testing purposes
		/note The current Playing song won't be included in the queue
		"""
		enqueued_sources = self._media_object.queue()
		paths = []
		for source in enqueued_sources:
			paths.append(source.fileName())
		return paths


	def get_playing_song(self):
		""" Return the current playing song for testing purposes """
		return self._media_object.currentSource().fileName()
